﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using DTN.Core.Common;

namespace DTN.Core.Utilities
{
    public class StringUtility
    {
        public static bool CheckTelcoCanCharge(string mobile)
        {
            bool retval = true;

            if (mobile != null && mobile.Length > 5)
            {
                string prefix = mobile.Substring(0, 5);
                retval = (prefix != "84199" && prefix != "84188");
            }
            
            if (mobile != null && mobile.Length > 4)
            {
                string prefix = mobile.Substring(0, 4);
                retval = (prefix != "8492");
            }
            return retval;
        }

        public static string DetectHyperLinkMobile(string content)
        {
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                       RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (urlregex.Match(content).Success)
            {
                foreach (Match m in urlregex.Matches(content))
                {
                    string v = m.Value;
                    string newv = v;
                    if (newv.Length > 18)
                        newv = newv.Substring(0, 18) + "...";
                    content = content.Replace(v, string.Format("<a href=\"{0}\" target=\"_blank\">{1}</a>", v, newv));
                }

            }
            return content;
        }
        public static string strProcess(string str, int length)
        {
            string retVal = String.Empty;
            if (str.Length > length)
            {
                str = str.Substring(0, length);
                for (int i = str.Length; i > 0; i--)
                {
                    retVal = str[i - 1].ToString();
                    if (retVal == " ")
                    {
                        retVal = str.Substring(0, i - 1).ToString() + "...";
                        break;
                    }
                }
                return retVal;
            }
            else
                return str;

        }

        public static string FormatBlance(double input)
        {
            string retVal = "";
            if (input == 0) retVal = "0 VND";
            else if (Math.Abs(input) >= 1000)
                retVal = string.Format("{0:0,0} VND", input);
            else retVal = string.Format("{0} VND", input);
            return retVal;
        }

        public static string BreakLongString(string oldString, int length)
        {
            int temp = 0;
            string ret = "";
            int temp1 = 6;
            length = length - temp1;

            for (int i = 0; i < oldString.Length; i++)
            {
                try
                {
                    if (temp == i)
                    {
                        string tempString = "";
                        temp = temp + length;
                        for (int j = i; j < temp; j++)
                        {
                            ret += oldString[j].ToString();
                            tempString += oldString[j].ToString();
                        }


                        //check 
                        bool needBreak = false;

                        if (tempString.IndexOf(" ") < 0)
                        {
                            temp = temp + temp1;
                            for (int k = i + length; k < temp; k++)
                            {
                                if (oldString[k].ToString() != " ")
                                {
                                    ret += oldString[k].ToString();
                                    needBreak = true;
                                }
                                else //stop check
                                {
                                    ret += oldString[k].ToString();
                                    temp = k;
                                    needBreak = false;
                                    break;
                                }
                            }
                        }

                        if (needBreak)
                        {
                            ret += "<br/> ";
                        }
                    }
                }
                catch
                { }
            }

            return ret;

        }


        public static string CleanNonAlphabetString(string input)
        {
            input = UnicodeToKoDau(input.ToLower());
            string output = Regex.Replace(input, @"[^A-Za-z0-9_\-]", "-", RegexOptions.IgnoreCase);
            return output;
        }

       
        public static string StripScriptTags(string content)
        {
            string cleanText;

            // Perform RegEx
            content = Regex.Replace(content, "<script((.|\n)*?)</script>", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            cleanText = Regex.Replace(content, "\"javascript:", "", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            return cleanText;
        }

        public static string StripHtmlXmlTags(string content)
        {
            return Regex.Replace(content, "<[^>]+>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }

        public static string UnHtmlEncode(string formattedPost)
        {
            RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;

            // &quot;
            // TODO: edit the regex match to only replace within a set of brackets [].
            //
            formattedPost = Regex.Replace(formattedPost, "&quot;", "\"", options);

            // Remove few other html encoded chars
            //
            formattedPost = Regex.Replace(formattedPost, "&lt;", "<", options);
            formattedPost = Regex.Replace(formattedPost, "&gt;", ">", options);

            return formattedPost;
        }

        public const string uniChars =
            "àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";

        public const string KoDauChars =
            "aaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU";

        public static string UnicodeToKoDau(string s)
        {
            string retVal = "";
            if (s == null)
                return retVal;
            int pos;
            for (int i = 0; i < s.Length; i++)
            {
                pos = uniChars.IndexOf(s[i].ToString());
                if (pos >= 0)
                    retVal += KoDauChars[pos];
                else
                    retVal += s[i];
            }
            return retVal;
        }

        public static string FilterHTMLContent(string input)
        {
            if (input.Trim() == string.Empty)
                return string.Empty;

            string retVal = string.Empty;

            string beginTag = "<!--start-->";
            int start = input.LastIndexOf("<!--start-->") + beginTag.Length;
            int end = input.LastIndexOf("<!--end-->");
            if (start > 0 && end > 0)
            {
                retVal = input.Substring(start, end - start);
            }
            if (retVal == "")
                retVal = input;
            return retVal;
        }

        public static string replateSize(string input)
        {
            if (input.Trim() == string.Empty)
                return string.Empty;

            string retVal = string.Empty;
            int lastBegin = 0;

            string htmltext = @"(?<begintag><font[^>]*size)=""*(?<size>\d)""*\s*>";
            MatchCollection mc = Regex.Matches(input, htmltext);
            foreach (Match mm in mc)
            {
                int beginSize = mm.Groups["size"].Index;
                int endSize = beginSize + mm.Groups["size"].Length;

                retVal += input.Substring(lastBegin, beginSize - lastBegin) + ConvertUtility.ToString(8 * ConvertUtility.ToInt32(mm.Groups["size"].ToString()));
                lastBegin = endSize;
            }
            retVal += input.Substring(lastBegin);

            return "<font size='24'>" + retVal + "</font>";
        }
    }
}
